from wormbane.engine.save_game import remove_save_dir, save_game
from wormbane.geometry import is_horizontal_direction, is_vertical_direction
from wormbane.ui.commands.climb import command_climb
from wormbane.ui.commands.close import command_close
from wormbane.ui.commands.drop import command_drop
from wormbane.ui.commands.eat import command_eat
from wormbane.ui.commands.inventory import command_inventory
from wormbane.ui.commands.look import command_look
from wormbane.ui.commands.move import command_move
from wormbane.ui.commands.open_ import command_open
from wormbane.ui.commands.remove import command_remove
from wormbane.ui.commands.search import command_search
from wormbane.ui.commands.take import command_take
from wormbane.ui.commands.wear import command_wear
from wormbane.ui.commands.wield import command_wield

def handle_command(command, game, ui):
    if is_horizontal_direction(command):
        command_move(command, game)
    elif is_vertical_direction(command):
        command_climb(command, game)
    elif command == 'close':
        command_close(game, ui)
    elif command == 'drop':
        command_drop(game, ui)
    elif command == 'eat':
        command_eat(game, ui)
    elif command == 'inventory':
        command_inventory(game, ui)
    elif command == 'look':
        command_look(game, ui)
    elif command == 'open':
        command_open(game, ui)
    elif command == 'quit':
        command_quit(game)
    elif command == 'remove':
        command_remove(game, ui)
    elif command == 'rest':
        command_rest(game)
    elif command == 'save':
        command_save(game)
    elif command == 'search':
        command_search(game)
    elif command == 'take':
        command_take(game, ui)
    elif command == 'wear':
        command_wear(game, ui)
    elif command == 'wield':
        command_wield(game, ui)

def command_quit(game):
    try:
        remove_save_dir()
    except OSError:
        pass
    game.quit = True
    game.quit_message = 'You quit.'

def command_rest(game):
    game.write(game.hero, 'rests')
    game.hero_moved = True

def command_save(game):
    save_game(game)
    game.quit = True
    game.quit_message = 'Your game was saved.'
